package com.ruoyi.bank.service.impl;

import java.util.List;

import com.ruoyi.bank.domain.BankGreyRegister;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanValidators;
import com.ruoyi.system.service.impl.SysUserServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.bank.mapper.BankVillagersMapper;
import com.ruoyi.bank.domain.BankVillagers;
import com.ruoyi.bank.service.IBankVillagersService;

import javax.validation.Validator;

/**
 * 村民信息管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-03-15
 */
@Service
public class BankVillagersServiceImpl implements IBankVillagersService 
{
    private static final Logger log = LoggerFactory.getLogger(SysUserServiceImpl.class);
    @Autowired
    private BankVillagersMapper bankVillagersMapper;
    @Autowired
    protected Validator validator;
    /**
     * 查询村民信息管理
     * 
     * @param villagerId 村民信息管理主键
     * @return 村民信息管理
     */
    @Override
    public BankVillagers selectBankVillagersByVillagerId(Long villagerId)
    {
        return bankVillagersMapper.selectBankVillagersByVillagerId(villagerId);
    }
    /**
     * 查询村民信息管理
     *
     * @param villagerNumber 村民信息管理主键
     * @return 村民信息管理
     */
    @Override
    public BankVillagers selectBankVillagersByVillagerNumber(String villagerNumber)
    {
        return bankVillagersMapper.selectBankVillagersByVillagerNumber(villagerNumber);
    }
    /**
     * 查询村民信息管理列表
     * 
     * @param bankVillagers 村民信息管理
     * @return 村民信息管理
     */
    @Override
    public List<BankVillagers> selectBankVillagersList(BankVillagers bankVillagers)
    {
        return bankVillagersMapper.selectBankVillagersList(bankVillagers);
    }
/**
 * 导入用户数据
 *
 * @param userList 用户数据列表
 * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
 * @param operName 操作用户
 * @return 结果
 */
    /**
     * 导入
     */

    @Override
    public String importvillagers(List<BankVillagers> villagersList, Boolean isUpdateSupport, String operName) {
        if (StringUtils.isNull(villagersList) || villagersList.isEmpty()) {
            throw new ServiceException("导入用户数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (BankVillagers bankVillagers : villagersList) {
            try {
                //检查必填项是否有空项
                if(bankVillagers.getVillageId() == null || bankVillagers.getVillagerDomicileNumber() =="" || bankVillagers.getVillagerName() == "" || bankVillagers.getVillagerSex() == "" || bankVillagers.getVillagerAge() == "" || bankVillagers.getRelationship() == "" || bankVillagers.getVillagerNumber() == ""){
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + ". "+ " 该条数据必填项未填写完整");
                    continue; // 跳过当前循环，处理下一条记录
                }
                // 新增检查身份证号长度是否为18位
                if (bankVillagers.getVillagerNumber().length() != 18) {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + ". "+bankVillagers.getVillagerName()+ "身份证号 " + bankVillagers.getVillagerNumber() + " 非法，身份证号必须为18位");
                    continue; // 跳过当前循环，处理下一条记录
                }
                // 新增检查手机号长度是否为11位
                if (bankVillagers.getVillagerPhonenumber().length() != 11) {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + ". "+bankVillagers.getVillagerName()+"身份证号 " + bankVillagers.getVillagerNumber() + " 的手机号非法，手机号必须为11位");
                    continue; // 跳过当前循环，处理下一条记录
                }
                // 验证是否存在这个项目编号
                BankVillagers existingBankVillagers = bankVillagersMapper.selectBankVillagersByVillagerNumber(bankVillagers.getVillagerNumber());
                if (StringUtils.isNull(existingBankVillagers)) {
                    BeanValidators.validateWithException(validator, bankVillagers);
                    bankVillagers.setCreateBy(operName);
                    bankVillagersMapper.insertBankVillagers(bankVillagers);
                    successNum++;
                    successMsg.append("<br/>" + successNum + ". "+bankVillagers.getVillagerName()+"身份证号 " + bankVillagers.getVillagerNumber() + " 导入成功");
                } else if (isUpdateSupport) {
                    BeanValidators.validateWithException(validator, bankVillagers);
                    bankVillagers.setUpdateBy(operName);
                    bankVillagersMapper.updateBankVillagers(bankVillagers);
                    successNum++;
                    successMsg.append("<br/>" + successNum + ". "+bankVillagers.getVillagerName()+"身份证号 " + bankVillagers.getVillagerNumber() + " 更新成功");
                } else {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + ". "+bankVillagers.getVillagerName()+"身份证号 " + bankVillagers.getVillagerNumber() + " 已存在");
                }
            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + ". "+bankVillagers.getVillagerName()+"身份证号 " + bankVillagers.getVillagerNumber() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            return failureMsg.toString(); // 修改此处以返回错误消息
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
            return successMsg.toString();
        }
    }

    /**
     * 新增村民信息管理
     * 
     * @param bankVillagers 村民信息管理
     * @return 结果
     */
    @Override
    public int insertBankVillagers(BankVillagers bankVillagers)
    {
        return bankVillagersMapper.insertBankVillagers(bankVillagers);
    }

    /**
     * 修改村民信息管理
     * 
     * @param bankVillagers 村民信息管理
     * @return 结果
     */
    @Override
    public int updateBankVillagers(BankVillagers bankVillagers)
    {
        return bankVillagersMapper.updateBankVillagers(bankVillagers);
    }

    /**
     * 批量删除村民信息管理
     * 
     * @param villagerIds 需要删除的村民信息管理主键
     * @return 结果
     */
    @Override
    public int deleteBankVillagersByVillagerIds(Long[] villagerIds)
    {
        return bankVillagersMapper.deleteBankVillagersByVillagerIds(villagerIds);
    }

    /**
     * 删除村民信息管理信息
     * 
     * @param villagerId 村民信息管理主键
     * @return 结果
     */
    @Override
    public int deleteBankVillagersByVillagerId(Long villagerId)
    {
        return bankVillagersMapper.deleteBankVillagersByVillagerId(villagerId);
    }
}
